﻿using Microsoft.AspNetCore.Mvc;
using OSMonitor.App.Core;
using OSMonitor.App.Models;
using System;
using System.Diagnostics;

namespace OSMonitor.App.Controllers
{
    public class HomeController : Controller
    {
        public IActionResult Index()
        {
            if (QuartzExtension.HasSet)
            {

                return View(new TimeModel { ShutDownTime = QuartzExtension.ExecutionTime, Message = "关机计划设定为" ,HasSet=true, HasExecuted= QuartzExtension.Executed});
            }
            else
            {
                return View(new TimeModel { ShutDownTime = QuartzExtension.ExecutionTime, Message = "请设定关机计划", HasSet = false });
            }
        }
        [HttpGet]
        public IActionResult SetTime()
        {
            if (QuartzExtension.HasSet)
            {
                return View("Index", new TimeModel { ShutDownTime = QuartzExtension.ExecutionTime, Message = "关机计划设定为", HasSet = true, HasExecuted = QuartzExtension.Executed });
            }
            else
            {
                return View("Index", new TimeModel { ShutDownTime = QuartzExtension.ExecutionTime, Message = "请设定关机计划" , HasSet = false});
            }
        }
        [HttpPost]
        public IActionResult SetTime(TimeModel model)
        {
            QuartzExtension.ExecutionTime = model.ShutDownTime;
            if (QuartzExtension.Scheduler())
            {
                QuartzExtension.Set();
                model.Message = "关机计划设定为";
                model.HasSet = true;
                return View("Index", model);
            }
            else
            {
                model.ShutDownTime = QuartzExtension.ExecutionTime;
                model.Message = "时间设置错误-请确保设置大于当前时间";
                model.HasSet = false;
                QuartzExtension.ExecutionTime = null;
                return View("Index", model);
            }
        }
        [HttpPost]
        public IActionResult CancelTime()
        {
            QuartzExtension.UnScheduler();
            return View("Index", new TimeModel { ShutDownTime = QuartzExtension.ExecutionTime, Message = "请设定关机计划",HasSet =false });
        }

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
    }
}
